package com.thenmorethings.action;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;
import com.thenmorethings.entity.PageImg;
import com.thenmorethings.service.ImgService;
import com.thenmorethings.util.uploadUtil;

public class AdminImgAction extends ActionSupport{

	@Resource
	private ImgService imgService;
	private File pageImg;
	private String pageImgContentType;
	private String pageImgFileName;
	private List<PageImg> imglist; 
	private PageImg pi;
	
	
	
	public String Imgupload()
	{
		PageImg pi = new PageImg();
		HttpServletRequest request = ServletActionContext.getRequest();
		
		try {
			if(pageImg != null)
			{
			  request.setCharacterEncoding("UTF-8");
			  String content = request.getParameter("pageContext");
			  String img_name = request.getParameter("imgname");
			  String filePath = ServletActionContext.getServletContext().getRealPath("upload/img");
			  String fileName = UUID.randomUUID().toString().replace("-", "") + pageImgFileName.substring(pageImgFileName.lastIndexOf("."));
			  FileUtils.copyFile(pageImg, new File(filePath,fileName));
			  
			  uploadUtil.upload(fileName, filePath+"/"+fileName);
			  
			  pi.setPath(fileName);
			  pi.setContent(content);
			  pi.setName(img_name);
			  imgService.save(pi);
			  return "uploadSuccess";
			}
			return "error";
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	public String loadImg()
	{
		imglist = imgService.findAll();
		return "Success";
	}
	/*
	 * 图片编辑跳转界面
	 */
	public String ImgeditorUI()
	{
		if(pi!=null)
		{
			pi = imgService.findById(pi.getId());
			
		}
		
		return "imgeditor";
	}
	public String ImgUpdate()
	{
		if(pi!=null)
		{
			imgService.updateImg(pi);
			
		}
		return "editor";
	}
	public String ImgDelete()
	{
		if(pi!=null)
		{
			imgService.deleteImg(pi.getId());
		}
		return "deleteSuccess";
	}

	
	
	
	public File getPageImg() {
		return pageImg;
	}
	public void setPageImg(File pageImg) {
		this.pageImg = pageImg;
	}
	public String getPageImgContentType() {
		return pageImgContentType;
	}
	public void setPageImgContentType(String pageImgContentType) {
		this.pageImgContentType = pageImgContentType;
	}
	public String getPageImgFileName() {
		return pageImgFileName;
	}
	public void setPageImgFileName(String pageImgFileName) {
		this.pageImgFileName = pageImgFileName;
	}
	public List<PageImg> getImglist() {
		return imglist;
	}
	public void setImglist(List<PageImg> imglist) {
		this.imglist = imglist;
	}
	public PageImg getPi() {
		return pi;
	}
	public void setPi(PageImg pi) {
		this.pi = pi;
	}
	
	
	
}
